home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2610 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  80 lines

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with c code, please help!
  5. Date: 22 Jan 1996 17:32:16 GMT
  6. Organization: OpenVision
  7. Message-ID: <4e0hn0$e80@spanky.pls.ov.com>
  8. References: <surgsw-1901960148530001@128.206.206.86>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article 1901960148530001@128.206.206.86, surgsw@mizzou1.missouri.edu (Joel Weinstein) writes:
  13. >I have been trying to get this very simple piece of code to work for
  14. >hours.  What is the problem???????
  15. >
  16. >#include <stdio.h>
  17. >
  18. >main()
  19. >{
  20. >   int i=0;
  21. >   char  word[100], c;
  22. >   printf("Enter a word:   ");
  23. >   while( (c = getchar()) != '\n')  {
  24. >      *word = c;
  25.        ^^^^^
  26. Problem #1, word is not a pointer.  You can't dereference it.  If you
  27. wish to use a pointer into the "word" array, you need to create and
  28. initialize the pointer.
  29.  
  30. >      word == word + 1;
  31.             ^^
  32. Problem #2, this is a test for equality.  In this case it will always
  33. test false, and does not affect the value in "word" (which is not a pointer).
  34.  
  35. >   }  
  36. >   printf("You entered:  ");  
  37. >   *word = 0;
  38. >   while( *word != '\n' )   
  39. >   {  
  40. >      putchar( *word );
  41. >      word == word + 1; 
  42. >   }
  43. >}  
  44. >
  45. >I think the problem I am having is due to my not knowing when to use the
  46. >'*' operator.  When do you use it?  Also, why won't my goddam compiler let
  47. >me do word++; instead of word == word +1;   I also tried to do *word++;
  48. >and it didn't work, what is the deal with that.  It won't let me put word
  49. >= word + 1;.  It insists on the double ='s.  Why is that?
  50. >
  51. >Please send e-mail.
  52. >
  53. >Joel
  54. >
  55. >ps: is there a way to find out what exactly error messages mean?  I kept
  56. >getting something similar to: not an Ivalue.  It would be nice if I knew
  57. >what the hell that meant.
  58.  
  59. A corrected version of your code is below:
  60.  
  61. #include <stdio.h>
  62.  
  63. main()
  64. {
  65.    int i=0, c;            /* getchar() returns an int */
  66.    char  word[100], *ptr; /* Defining a pointer here */
  67.    ptr = word;            /* point to the first character of "word" */
  68.    printf("Enter a word:   ");
  69.    while( (c = getchar()) != '\n')  {
  70.       *ptr = (char) c;    /* put the data into the array */
  71.       ptr++;              /* point to the next location to be filled */
  72.    }
  73.    *ptr = '\0';           /* terminate the string */
  74.    printf("You entered:  %s\n", word);  
  75. }
  76.  
  77.             Fletcher.Glenn@ov.com  
  78.  
  79.  
  80.